package com.hm.manage.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Slf4j
@Component
public class HikArtemisUtiles {

    @Value("${hikvision.artemis.baseurl}")
    private String baseurl;

    @Value("${hikvision.artemis.appKey}")
    private String appKey;

    @Value("${hikvision.artemis.appSecret}")
    private String appSecret;

    private static final String ARTEMIS_PATH = "/artemis";

    public boolean isSuccess(String s){
        JSONObject jsonObject = JSON.parseObject(s);
        if (jsonObject.get("code").equals("0")){
            return true;
        } else {
            return false;
        }
    }

    public String getDate(String s){
        return JSON.parseObject(s).get("data").toString();
    }

    public  Integer getCurrentVehiclePassCount(){
        log.info("---------------开始执行ArtemisTask.getCurrentVehiclePassCount----{}--{}--{}---------",baseurl,appKey,appSecret);
        // 获取当前时间
        String ydm = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 创建ArtemisConfig对象，并设置相关参数
        ArtemisConfig artemisConfig = new ArtemisConfig();
        artemisConfig.setHost(baseurl);
        artemisConfig.setAppKey(appKey);
        artemisConfig.setAppSecret(appSecret);

        // 按车辆属性查询过车信息 拼接请求的url  https://50.58.191.80:443/artemis/api/aiapplication/v1/vehicle/queryVehiclesByAttrWithPage
        final String getCamsApi = ARTEMIS_PATH + "/api/aiapplication/v1/vehicle/queryVehiclesByAttrWithPage";

        // 构造请求的地址
        HashMap<String, String> path = new HashMap<>(2) {
            {
                put("https://", getCamsApi);
            }
        };

        // 构造请求参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo",1);
        jsonBody.put("pageSize",1);
        jsonBody.put("beginTime",ydm+"T00:00:00.000+08:00");
        jsonBody.put("endTime",ydm+"T08:30:00.000+08:00");
        String body = JSON.toJSONString(jsonBody);

        // 发送请求
        String s = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json",null);
        if (!isSuccess(s)) {
            log.error("-------------接口调用异常-------------");
        }
        Object total = JSON.parseObject(getDate(s)).get("total");
        log.info("-----------请求返回结果：------{}",total);
        int integer = Integer.parseInt(total.toString());

        log.info("---------------结束执行ArtemisTask.getCurrentVehiclePassCount-----------------");
        return integer;
    }

    /***
     * 获取指定时间的过车数量
     * 开始时间和结束时间
     * @return
     */
    public  Integer getCurrentVehiclePassCountByTime(String beginTime,String endTime) {
        log.info("---------------开始执行ArtemisTask.getCurrentVehiclePassCountByTime----{}--{}--{}---------",baseurl,appKey,appSecret);
        // 获取当前时间
        //String ydm = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 创建ArtemisConfig对象，并设置相关参数
        ArtemisConfig artemisConfig = new ArtemisConfig();
        artemisConfig.setHost(baseurl);
        artemisConfig.setAppKey(appKey);
        artemisConfig.setAppSecret(appSecret);

        // 按车辆属性查询过车信息 拼接请求的url  https://50.58.191.80:443/artemis/api/aiapplication/v1/vehicle/queryVehiclesByAttrWithPage
        final String getCamsApi = ARTEMIS_PATH + "/api/aiapplication/v1/vehicle/queryVehiclesByAttrWithPage";

        // 构造请求的地址
        HashMap<String, String> path = new HashMap<>(2) {
            {
                put("https://", getCamsApi);
            }
        };

        // 构造请求参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo",1);
        jsonBody.put("pageSize",1);
        jsonBody.put("beginTime",beginTime);
        jsonBody.put("endTime",endTime);
        String body = JSON.toJSONString(jsonBody);

        // 发送请求
        String s = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json",null);
        if (!isSuccess(s)) {
            log.error("-------------接口调用异常-------------");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            s = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json",null);
            if (!isSuccess(s)) {
                log.error("-------------二次接口调用异常-------------");
                return 0;
            }
        }
        Object total = JSON.parseObject(getDate(s)).get("total");
        log.info("-----------请求返回结果：------{}",total);
        int integer = Integer.parseInt(total.toString());

        log.info("---------------结束执行ArtemisTask.getCurrentVehiclePassCountByTime-----------------");
        return integer;
    }


}
